package com.steven.ourcontest.report.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.steven.ourcontest.base.BaseViewModel
import com.steven.ourcontest.chat.bean.ReportRes
import com.steven.ourcontest.forum.api.ForumApi
import com.steven.ourcontest.net.EmptyResponse
import com.steven.ourcontest.utils.plusAssign

class HandleReportViewModel : BaseViewModel() {

    private val _reportRes = MutableLiveData<ReportRes?>()
    val reportRes: LiveData<ReportRes?>
        get() = _reportRes

    private val _updateRes = MutableLiveData<EmptyResponse?>()
    val updateRes: LiveData<EmptyResponse?>
        get() = _updateRes

    private var lastCreateTime = Long.MAX_VALUE
    fun refresh() {
        lastCreateTime = Long.MAX_VALUE
        queryReport()
    }

    fun loadMore() {
        queryReport()
    }

    private fun queryReport() {
        disposed += ForumApi.instance.queryComplain(lastCreateTime)
            .subscribe({
                _reportRes.value = it
                it.data.lastOrNull()?.createTime?.let { time ->
                    lastCreateTime = time
                }
            }, {
                _reportRes.value = null
                it.printStackTrace()
            })
    }

    fun updateReport(id: Long) {
        disposed += ForumApi.instance.updateComplain(id)
            .subscribe({
                _updateRes.value = it
            }, {
                it.printStackTrace()
            })
    }
}